#include "DynamicArray.h"
#include <stdlib.h>
#include <stdio.h>

void InitDArray(DArray *da)//数组初始化
{
    da->size = 10;
    da->a = (ElementType*)malloc(sizeof(ElementType)* da->size);
    if(da->a == NULL)
    {
        printf("malloc error!\n");
        return;
    }
    da->len = 0;//初始化还没有元素

}

void InsertTail(DArray * da, ElementType element)
{
    //数组满了
    if(da->len == da->size)
    {
        da->size *= 2;
        ElementType *newP = (ElementType *)malloc(sizeof(ElementType)*da->size);
        if(newP == NULL)
        {
            printf("malloc error!\n");
            return;
        }
        for(int i = 0; i < da->len;i++)
        {
            newP[i] = da->a[i];
          
        }
        free(da->a);
        da->a = newP;

    }
    da->a[da->len++] = element;//da->a[da->len] = element; da->len++;
  //尾插法
}

void PrintDArray(DArray *da)
{
    printf("len = %d size = %d\n",da->len,da->size);
    for(int i = 0; i < da->len;i++)
    {
        printf("%d ",da->a[i]);
    }
    printf("\n");
}
